//
//  LGPatientHomeCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/23.
//  患者列表 -- Cell

#import "LGPatientHomeCell.h"

@interface LGPatientHomeCell()
/**   */
@property (nonatomic , strong)UILabel *nameLabel;
/**   */
@property (nonatomic , strong)UIImageView *vipView;
/**   */
@property (nonatomic , strong)UIButton *doctorBtn;
/**   */
@property (nonatomic , strong)UILabel *focusLabel;
/**   */
@property (nonatomic , strong)UILabel *operatLabel;
/**   */
@property (nonatomic , strong)UIView *itemView;
/**   */
@property (nonatomic , strong)UILabel *ageLabel;
/**  最新计划 */
@property (nonatomic , strong)UILabel *planLabel;
/**  最新计划 */
@property (nonatomic , strong)UIView *lineView;
/**   */
@property (nonatomic , strong)NSMutableArray *operateArray;
/**   */
@property (nonatomic , strong)NSDictionary *vipWidth;

@end

@implementation LGPatientHomeCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.vipWidth = @{@"1":@(16),@"2":@(32),@"3":@(48),@"4":@(64)};
        [self createSubView];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.nameLabel.text = LGNSString(dataDic[@"realname"]);
    NSString *vipLevel = LGNSString(dataDic[@"vip_level"]);
    self.vipView.image = [UIImage imageNamed:[NSString stringWithFormat:@"vip_icon_%@",vipLevel]];
    CGFloat vipWidth = [self.vipWidth.allKeys containsObject:vipLevel]?[self.vipWidth[vipLevel] floatValue]:0;
    [self.vipView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(vipWidth));
    }];
    CGFloat left = ([vipLevel integerValue] > 0)?(viewPix(12)+vipWidth):viewPix(6);
    NSString *doctorName = LGNSString(dataDic[@"doctor_name"]);
    CGFloat width = [doctorName textWidthWithFont:LGFontWeight(12, UIFontWeightMedium) maxHeight:MAXFLOAT];
    width = (doctorName.length>0)?width+viewPix(35):0;
    [self.doctorBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(left);
        make.width.equalTo(@(width));
    }];
    [self.doctorBtn setTitle:doctorName forState:UIControlStateNormal];
    self.doctorBtn.hidden = (doctorName.length>0)?NO:YES;
    //是否关注
    CGFloat focusLeft = (doctorName.length > 0)?viewPix(4):0;
    self.focusLabel.hidden = ![dataDic[@"is_focus"] boolValue];
    [self.focusLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.doctorBtn.mas_right).offset(focusLeft);
    }];
    //手术日期
    NSString *operateDate = LGNSString(dataDic[@"surgery_date"]);
    NSString *textColor = (operateDate.length>0)?@"#333333":@"#999999";
    self.operatLabel.textColor = [UIColor colorWithString:textColor];
    self.operatLabel.text = (operateDate.length>0)?[NSString stringWithFormat:@"%@手术",operateDate]:@"";
    
    //手术列表item
    NSArray *itemArray = dataDic[@"tips"];
    for (UILabel *item in self.operateArray) {
        [item removeFromSuperview];
    }
    [self.operateArray removeAllObjects];
    CGFloat itemTop = 0;
    CGFloat itemLeft = 0;
    for (NSInteger i=0; i<itemArray.count; i++) {
        CGFloat itemWidth = [itemArray[i] textWidthWithFont:LGFontWeight(12, UIFontWeightMedium) maxHeight:MAXFLOAT];
        itemTop = ((itemLeft+itemWidth+viewPix(16))>=Screen_W-viewPix(32)) ? itemTop+viewPix(24):itemTop;
        itemLeft = ((itemLeft+itemWidth+viewPix(16))>=Screen_W-viewPix(32)) ? 0:itemLeft;
        UILabel *itemLabel = [UILabel lableWithText:itemArray[i] colorString:@"#4C87FD" textFont:LGFontWeight(12, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        itemLabel.frame = CGRectMake(itemLeft, itemTop, itemWidth+viewPix(16), viewPix(16));
        itemLabel.layer.borderColor = [UIColor colorWithString:@"#4C87FD"].CGColor;
        itemLabel.layer.borderWidth = 1;
        itemLabel.cornerRidus = viewPix(8);
        [self.itemView addSubview:itemLabel];
        [self.operateArray addObject:itemLabel];
        itemLeft += itemWidth+viewPix(20);
    }
    CGFloat itemHeight = (itemArray.count>0) ? itemTop+viewPix(16) : 0;
    [self.itemView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(itemHeight));
    }];
    
    //性别、年龄、病案号
    NSString *gender = LGNSString(dataDic[@"gender"]);
    NSString *age = LGNSString(dataDic[@"age"]);
    NSString *number = LGNSString(dataDic[@"hospital_bano"]);
    if(gender.length>0 && age.length >0){
        self.ageLabel.text = [NSString stringWithFormat:@"%@ %@岁 %@",gender,age,number];
    }else if (gender.length > 0 && age.length == 0){
        self.ageLabel.text = [NSString stringWithFormat:@"%@ %@",gender,number];
    }else if (gender.length == 0 && age.length>0){
        self.ageLabel.text = [NSString stringWithFormat:@"%@岁 %@",age,number];
    }else{
        self.ageLabel.text = [NSString stringWithFormat:@"%@",number];
    }
    CGFloat ageTop = (itemArray.count>0)?viewPix(8):0;
    [self.ageLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.itemView.mas_bottom).offset(ageTop);
    }];
    
    //最新计划
    CGFloat planTop = (self.ageLabel.text.length>0)?viewPix(8):0;
    [self.planLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.ageLabel.mas_bottom).offset(planTop);
    }];
    NSString *content = ([dataDic[@"care_plan"] count] > 0)?[NSString stringWithFormat:@"最新计划 %@ %@",dataDic[@"care_plan"][@"plan_date"],dataDic[@"care_plan"][@"type_name"]] : @"";
    self.planLabel.attributedText = [content attributeString:@{NSForegroundColorAttributeName:[UIColor colorWithString:@"#666666"]} range:NSMakeRange(0, 4)];
}


#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.vipView];
    [self.contentView addSubview:self.doctorBtn];
    [self.contentView addSubview:self.focusLabel];
    [self.contentView addSubview:self.operatLabel];
    [self.contentView addSubview:self.itemView];
    [self.contentView addSubview:self.ageLabel];
    [self.contentView addSubview:self.planLabel];
    [self.contentView addSubview:self.lineView];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(10));
        make.left.equalTo(self.contentView).offset(viewPix(16));
    }];
    
    [self.vipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(viewPix(6));
        make.centerY.equalTo(self.nameLabel);
        make.width.equalTo(@(16));
        make.height.equalTo(@(16));
    }];
    
    [self.doctorBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(viewPix(6));
        make.centerY.equalTo(self.nameLabel);
        make.width.equalTo(@(viewPix(70)));
        make.height.equalTo(@(viewPix(22)));
    }];
    
    [self.focusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.doctorBtn.mas_right).offset(viewPix(4));
        make.centerY.equalTo(self.nameLabel);
        make.width.equalTo(@(viewPix(52)));
        make.height.equalTo(@(viewPix(22)));
    }];
    
    [self.operatLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-viewPix(16));
        make.centerY.equalTo(self.nameLabel);
    }];
    
    [self.itemView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(viewPix(8));
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.right.equalTo(self.contentView).offset(-viewPix(16));
    }];
    [self.ageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel);
        make.top.equalTo(self.itemView.mas_bottom).offset(viewPix(8));
    }];
    
    [self.planLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel);
        make.top.equalTo(self.ageLabel.mas_bottom).offset(viewPix(8));
        make.bottom.equalTo(self.contentView).offset(-viewPix(10));
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.right.equalTo(self.contentView).offset(-viewPix(16));
        make.bottom.equalTo(self.contentView);
        make.height.equalTo(@(0.5));
    }];
}

-(NSMutableArray *)operateArray{
    if(!_operateArray){
        _operateArray = [NSMutableArray array];
    }
    return _operateArray;
}

-(UILabel *)nameLabel{
    if(!_nameLabel){
        _nameLabel = [UILabel lableWithText:@"姓名" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _nameLabel;
}

-(UIImageView *)vipView{
    if(!_vipView){
        _vipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"vip_icon_1"]];
    }
    return _vipView;
}

-(UIButton *)doctorBtn{
    if(!_doctorBtn){
        _doctorBtn = [UIButton buttonWithTitle:@"" titleFont:LGFontWeight(12, UIFontWeightMedium) textColor:@"#56BEA0" imageName:@"药球患者-医生"];
        _doctorBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _doctorBtn.imageEdgeInsets = UIEdgeInsetsMake(0, viewPix(10), 0, 0);
        _doctorBtn.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(12), 0, 0);
        _doctorBtn.backgroundColor = [UIColor colorWithString:@"#EBF8F3"];
        _doctorBtn.userInteractionEnabled = NO;
        _doctorBtn.cornerRidus = viewPix(11);
    }
    return _doctorBtn;
}

-(UILabel *)focusLabel{
    if(!_focusLabel){
        _focusLabel = [UILabel lableWithText:@"已关注" colorString:@"#E3516B" textFont:LGFontWeight(12, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _focusLabel.backgroundColor = [UIColor colorWithString:@"#FFDAE0"];
        _focusLabel.cornerRidus = viewPix(11);
    }
    return _focusLabel;
}

-(UILabel *)operatLabel{
    if(!_operatLabel){
        _operatLabel = [UILabel lableWithText:@"" colorString:@"999999" textFont:LGFont(14) textAlignment:NSTextAlignmentRight lines:1];
    }
    return _operatLabel;
}

-(UIView *)itemView{
    if(!_itemView){
        _itemView = [[UIView alloc] init];
    }
    return _itemView;
}

-(UILabel *)ageLabel{
    if(!_ageLabel){
        _ageLabel = [UILabel lableWithText:@"" colorString:@"#999999" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _ageLabel;
}

-(UILabel *)planLabel{
    if(!_planLabel){
        _planLabel = [UILabel lableWithText:@"最新计划" colorString:@"#333333" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _planLabel;
}

-(UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor colorWithString:@"#EDF5FF"];
    }
    return _lineView;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
